import MockData from '../api.data';
import Constants from '../utils/constants';

function HttpMockDataInterceptor (request, next) {

	var fullUrl = request.url, 
		path = fullUrl.replace(Constants.BASE_URL, '');
		
	path = path.substring(0, path.indexOf('?') === -1 ? path.length : path.indexOf('?'));

	if (!MockData.disabled && MockData[path] && !MockData[path].disabled) {
		var result = MockData[path][request.method.toLowerCase()];

		if (!MockData.debug) {
			console.warn(request.url, request.method, request.body, result);
		}

		next(request.respondWith(result, {
			status: 200
		}));
	} else {
		next();
	}
}

export {HttpMockDataInterceptor};
